
#import "TorrentListViewCell.h"

@implementation TorrentListViewCell

//
// name
//
@synthesize nameLabel														= _nameLabel;

//
// time
//
@synthesize timeLabel														= _timeLabel;

//
// category
//
@synthesize categoryLabel													= _categoryLabel;

//
// size
//
@synthesize sizeLabel														= _sizeLabel;

//
// up rate
//
@synthesize upRateImageView													= _upRateImageView;

//
// down rate
//
@synthesize downRateImageView												= _downRateImageView;

//
// unread
//
@synthesize unreadImageView													= _unreadImageView;

//
// sticky
//
@synthesize stickyImageView													= _stickyImageView;

//
// init
//
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
	//
	// call super
	//
	self																	= [super initWithStyle:style reuseIdentifier:reuseIdentifier];
	if(!self)
		return nil;
	
	//
	// create name label
	//
	self.nameLabel															= [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	self.nameLabel.textAlignment											= UITextAlignmentLeft;
	self.nameLabel.lineBreakMode											= UILineBreakModeTailTruncation;
	self.nameLabel.font														= [UIFont boldSystemFontOfSize:17];
	self.nameLabel.backgroundColor											= [UIColor clearColor];
	[self.contentView addSubview:self.nameLabel];
	
	//
	// create time label
	//
	self.timeLabel															= [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	self.timeLabel.textColor												= [UIColor grayColor];
	self.timeLabel.textAlignment											= UITextAlignmentLeft;
	self.timeLabel.lineBreakMode											= UILineBreakModeTailTruncation;
	self.timeLabel.font														= [UIFont systemFontOfSize:14];
	self.timeLabel.backgroundColor											= [UIColor clearColor];
	[self.contentView addSubview:self.timeLabel];
	
	//
	// create category label
	//
	self.categoryLabel														= [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	self.categoryLabel.textColor											= [UIColor colorWithRed:119 / 255.f green:144 / 255.f blue:159 / 255.f alpha:1];
	self.categoryLabel.textAlignment										= UITextAlignmentLeft;
	self.categoryLabel.lineBreakMode										= UILineBreakModeTailTruncation;
	self.categoryLabel.font													= [UIFont systemFontOfSize:14];
	self.categoryLabel.backgroundColor										= [UIColor clearColor];
	[self.contentView addSubview:self.categoryLabel];
	
	//
	// create size label
	//
	self.sizeLabel															= [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	self.sizeLabel.textColor												= [UIColor colorWithRed:168 / 255.f green:110 / 255.f blue:43 / 255.f alpha:1];
	self.sizeLabel.textAlignment											= UITextAlignmentRight;
	self.sizeLabel.lineBreakMode											= UILineBreakModeTailTruncation;
	self.sizeLabel.font														= [UIFont systemFontOfSize:14];
	self.sizeLabel.backgroundColor											= [UIColor clearColor];
	[self.contentView addSubview:self.sizeLabel];
	
	//
	// create unread image view
	//
	self.unreadImageView													= [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease];
	self.unreadImageView.backgroundColor									= [UIColor clearColor];
	[self.contentView addSubview:self.unreadImageView];
	
	//
	// create sticky image view
	//
	self.stickyImageView													= [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease];
	self.stickyImageView.backgroundColor									= [UIColor clearColor];
	[self.contentView addSubview:self.stickyImageView];
	
	//
	// create up rate image view
	//
	self.upRateImageView													= [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease];
	self.upRateImageView.backgroundColor									= [UIColor clearColor];
	[self.contentView addSubview:self.upRateImageView];
	
	//
	// create down rate image view
	//
	self.downRateImageView													= [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease];
	self.downRateImageView.backgroundColor									= [UIColor clearColor];
	[self.contentView addSubview:self.downRateImageView];
	
	return self;
}

//
// dealloc
//
- (void)dealloc
{
	self.nameLabel															= nil;
	self.timeLabel															= nil;
	self.categoryLabel														= nil;
	self.sizeLabel															= nil;
	self.upRateImageView													= nil;
	self.downRateImageView													= nil;
	self.unreadImageView													= nil;
	self.stickyImageView													= nil;
	
	[super dealloc];
}

//
// set image
//
- (void)setupImageWithUnread:(BOOL)unread sticky:(BOOL)sticky upRate:(NSString*)upRate downRate:(NSString*)downRate
{
	if(upRate.length && downRate.length)
	{
		self.upRateImageView.image											= [self imageFromText:upRate upload:YES];
		self.downRateImageView.image										= [self imageFromText:downRate upload:NO];
	}
	else if(upRate.length && !downRate.length)
	{
		self.upRateImageView.image											= nil;
		self.downRateImageView.image										= [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", upRate]];
	}
	else
	{
		self.upRateImageView.image											= nil;
		self.downRateImageView.image										= nil;
	}
	self.unreadImageView.image												= unread ? [UIImage imageNamed:@"unread.png"] : nil;
	self.stickyImageView.image												= sticky ? [UIImage imageNamed:@"sticky.png"] : nil;
	self.nameLabel.textColor												= unread ? [UIColor colorWithRed:180 / 255.f green:66 / 255.f blue:22 / 255.f alpha:1] : [UIColor blackColor];
}

//
// layout subviews
//
- (void)layoutSubviews
{
	[super layoutSubviews];
	
	CGRect bounds															= self.contentView.bounds;
	
	CGRect stickyFrame														= CGRectMake(2, 6, 14, 14);
	self.stickyImageView.frame												= stickyFrame;
	
	CGFloat x																= stickyFrame.origin.x + stickyFrame.size.width;
	CGRect nameFrame														= CGRectMake(x, 2, bounds.size.width - x - 2, 20);
	self.nameLabel.frame													= nameFrame;
	
	CGRect timeFrame														= CGRectMake(x, nameFrame.origin.y + nameFrame.size.height, nameFrame.size.width - 76, 18);
	self.timeLabel.frame													= timeFrame;
	CGRect unreadFrame														= CGRectMake(nameFrame.origin.x + nameFrame.size.width - 36 - 2, timeFrame.origin.y, 36, 19);
	self.unreadImageView.frame												= unreadFrame;
	
	CGRect categoryFrame													= CGRectMake(x, timeFrame.origin.y + timeFrame.size.height, nameFrame.size.width / 4, 18);
	self.categoryLabel.frame												= categoryFrame;
	CGRect sizeFrame														= CGRectMake(x + categoryFrame.size.width, categoryFrame.origin.y, nameFrame.size.width / 4, 18);
	self.sizeLabel.frame													= sizeFrame;
	
	CGRect downRateFrame													= CGRectMake(unreadFrame.origin.x, sizeFrame.origin.y, 36, 19);
	self.downRateImageView.frame											= downRateFrame;
	CGRect upRateFrame														= CGRectMake(downRateFrame.origin.x - 37, downRateFrame.origin.y, 36, 19);
	self.upRateImageView.frame												= upRateFrame;
}

//
// row height
//
+ (CGFloat)rowHeight
{
	return 62;
}

//
// create image from text
//
- (UIImage*)imageFromText:(NSString*)text upload:(BOOL)upload
{
	UIImage* backImage														= [UIImage imageNamed:upload ? @"OtherUp.png" : @"OtherDown.png"];
	UIFont* font															= [UIFont boldSystemFontOfSize:14];
	CGSize textSize															= [text sizeWithFont:font];
	
	if(UIGraphicsBeginImageContextWithOptions)
		UIGraphicsBeginImageContextWithOptions(backImage.size, NO, 0);
	else
		UIGraphicsBeginImageContext(backImage.size);
	
	[backImage drawAtPoint:CGPointZero];
	CGContextSetRGBFillColor(UIGraphicsGetCurrentContext(), 1, 1, 1, 1);
	[text drawAtPoint:CGPointMake((backImage.size.width - textSize.width) / 2, (backImage.size.height - textSize.height) / 2) withFont:font];
	UIImage* image															= UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	return image;
}
@end
